package vcp.scheduledtasks;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.LinkedList;

import db.parking.beans.Orders;
import db.parking.beans.Sms;
import db.tables.AdminCanceledOrders;
import db.tables.AdminOrders;
import db.tables.AdminSMS;

public class smsThread implements Runnable
{
  private String msg="Please Tell Us If Your On Your Way?";  
  // Constructor
  public smsThread()
  {

  }
	
  //The run method; when this finishes, the thread terminates
  public void run()
  {
	  Orders [] lateArray,cancelArray;
	  Sms sms = new Sms();
	  Sms [] answerArr;
	  long delay = 10000;
	  try
	    {
	      while (true)
	      {
	    	long time = System.currentTimeMillis();// Set current time at this second to StartTime
			Timestamp currTimestamp = new Timestamp(time);
			Timestamp cancelStamp = new Timestamp(time);
			cancelStamp.setTime(currTimestamp.getTime() - 1800000); 
		
	       
	        lateArray = AdminOrders.getLateOrders(currTimestamp);
	        if(lateArray.length > 0)
	        {
	        	for(int i=0;i<lateArray.length;i++)
	        	{
	        		if(!AdminSMS.smsSent(lateArray[i].getOid()) && !AdminSMS.smsExists(lateArray[i].getOid()) )
	        		{
	        					sms.setCusID(lateArray[i].getUid());
	        					sms.setoID(lateArray[i].getOid());
	        					sms.setMessage(msg);
	        					sms.setTimeSent(currTimestamp);
	        					sms.setStatus(1);
	        	        		AdminSMS.insert(sms, msg, lateArray[i].getStartTime());//send sms to user and set status to 1 as sms sent
	        		}
	        	}
	        }
	        
	        
	        answerArr = AdminSMS.getSmsAnswer();
	        
	        if(answerArr.length > 0)
	        {
	        	for(int i=0;i<answerArr.length;i++)
	        	{
	        				answerArr[i].setStatus(4);//set 4 to give customer a fine for being late
	        				AdminSMS.update(answerArr[i].getoID(), 4);
	        	}
	        	
	        }
	        	
	        
	        cancelArray = AdminOrders.getOrdersToCancel(cancelStamp);
	        //cancel order if 30 min passed and no message received
	        if(cancelArray.length > 0)
	        {
	        	for(int i=0;i<cancelArray.length;i++)
	        	{
	        		//Status is 1 , meaning sms has been sent and there is no reply
	        		if(AdminSMS.smsSent(lateArray[i].getOid()) && !AdminSMS.msgSent(lateArray[i].getOid()))
	        		{
	        				cancelArray[i].setStatus(3);
	        	        	AdminOrders.update(cancelArray[i]);//send sms to user and set status to 1 as sms sent
	        	        	AdminCanceledOrders.insert(cancelArray[i]);
	        	        	AdminSMS.update(cancelArray[i].getOid(),3);
	        		}
	        	}
	        }
	        
	        
	        
	        	
	        Thread.sleep(delay);
	      }
	    }
	    catch(InterruptedException e) {} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
  }

}